package com.example.onlineeducation.ui.mine


import android.content.Context
import android.content.Intent
import android.view.View
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.constant.StudyConstants
import com.example.onlineeducation.data.entity.TopicEntity
import com.example.onlineeducation.databinding.TopicChildItemBinding
import com.example.onlineeducation.databinding.WrongTopicLearningActivityBinding
import com.example.onlineeducation.ui.topic.StudyContentActivity
import com.example.onlineeducation.utils.YcTimeUtils
import com.example.onlineeducation.viewmodel.WrongTopicLearningVM
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.refresh.YcRefreshSpecialUtil
import com.yc.jetpacklib.release.YcSpecialViewSmart

/*
* 错题学习列表
* */
class WrongTopicLearningActivity : BaseActivity<WrongTopicLearningActivityBinding>(
    WrongTopicLearningActivityBinding::inflate
) {
    private val mViewModel: WrongTopicLearningVM by ycViewModels()

    private var mProId: String? = App.mInstant.mViewModel.mProInfoEntity.value?.proId
    private lateinit var mRefreshUtil: YcRefreshSpecialUtil

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, WrongTopicLearningActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mTopicAdapter by YcRecyclerViewAdapter.ycLazyInitApply<TopicEntity, TopicChildItemBinding>(
        TopicChildItemBinding::inflate
    ) {
        mOnUpdate = {
            ivStudy.ycLoadImageNet(it.examImage, errorImg = R.drawable.ic_topic)
            tvStudyName.text = it.topicName
            tvTotalGrade.visibility = View.INVISIBLE
            tvStudyTime.text = "已学习" + YcTimeUtils.intSecondToMinStr(it.topicStudyTime.toInt())
            tvState.text = StudyConstants.showStudyResultText(it.topicPass)
            tvState.ycSetTextColorRes(StudyConstants.showStudyResultTextColor(it.topicPass))
            ivState.setImageResource(StudyConstants.showStudyResultIcon(it.topicPass))
        }
        mItemClick = {
            StudyContentActivity.newInstance(
                this@WrongTopicLearningActivity,
                it.topicId,
                it.needStudyTime,
                it.topicStudyTime
            )
        }
    }

    override fun WrongTopicLearningActivityBinding.initView() {
        actionbarLayout.setTitle("错题学习")

        rvList.adapter = mTopicAdapter
        mRefreshUtil = YcRefreshSpecialUtil(
            this@WrongTopicLearningActivity,
            srlList,
            mTopicAdapter,
            YcSpecialViewSmart(rvList, flRefresh),
            false
        )

        srlList.setNoMoreData(true)
        mRefreshUtil.mRefreshAndLoadMore = { isRefresh: Boolean, pageIndex: Int, pageSize: Int ->
            if (isRefresh) {
                getData()
            }
        }
        mViewModel.mErrorListDataModel.observe {
            it.doSuccess { data ->
                mTopicAdapter.addAllData(data)
            }.ycShowNetError()
            mRefreshUtil.ycRefreshAndLoadMoreResult(it)
        }


        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null && mProId != it.proId) {
                mProId = it.proId
                getData()
            }
        }
        mRefreshUtil.startRefresh()
    }

    private fun getData() {
        mViewModel.getErrorList(mProId!!)

    }


}
